package com.mvc.cms.action.admin.assist;

import static com.mvc.common.page.SimplePage.cpn;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.cms.entity.main.CmsUser;
import com.mvc.cms.manager.assist.CmsHouseDataMng;
import com.mvc.cms.web.CmsUtils;
import com.mvc.common.page.Pagination;
import com.mvc.common.web.CookieUtils;

@Controller
public class CmsHouseDataAct {
    /**
     * 房屋数据分页显示
     * @param modelMap
     * @return
     */
	@RequestMapping("/house_data/v_list.do")
	public String list(String name,String area,String seller,Integer pageNo,
			String license,String use,String building,Date begin,Date end,
			  HttpServletRequest request,ModelMap modelMap){
		Pagination pagination = cmsHouseDataMng.getPage(name,area,seller,license,use,building,
				    begin,end,cpn(pageNo), CookieUtils.getPageSize(request));
		modelMap.put("pagination", pagination);
		modelMap.put("pageNo", pagination.getPageNo());
		modelMap.put("name", name);
		modelMap.put("area", area);
		modelMap.put("seller", seller);
		modelMap.put("license", license);
		modelMap.put("use", use);
		modelMap.put("building", building);
		
		
		modelMap.put("name",cmsHouseDataMng.getAllRow("name") );
		modelMap.put("area",cmsHouseDataMng.getAllRow("area") );
		modelMap.put("seller",cmsHouseDataMng.getAllRow("seller") );
		modelMap.put("use",cmsHouseDataMng.getAllRow("use") );
		modelMap.put("building",cmsHouseDataMng.getAllRow("building") );
		modelMap.put("covered",cmsHouseDataMng.getAllRow("covered") );
		modelMap.put("sets",cmsHouseDataMng.getAllRow("sets") );
		modelMap.put("turnover",cmsHouseDataMng.getAllRow("turnover") );
		modelMap.put("surface",cmsHouseDataMng.getAllRow("surface") );
		modelMap.put("sleeve",cmsHouseDataMng.getAllRow("sleeve") );
		modelMap.put("contractTime",cmsHouseDataMng.getAllRow("contractTime") );
		return "house/list";
	}
	
	@RequestMapping("/house_data/v_add.do")
	public String add(){
		return "house/add";
	}
	
	/**
	 * execl导入数据
	 * @param attachmentPaths
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/house_data/v_excelAdd.do")
    public String excelAdd(String attachmentPaths,HttpServletRequest request,
    		          ModelMap modelMap){
		CmsUser user = CmsUtils.getUser(request);
		String dir="";
    	Properties props = System.getProperties();
		StringBuilder builder = new StringBuilder();
		builder.append(props.getProperty("java.io.tmpdir")).
		     delete(props.getProperty("java.io.tmpdir").length()-5, props.getProperty("java.io.tmpdir").
		     length()).append("\\webapps").append(dir).append(attachmentPaths.replace("/", "\\"));
		cmsHouseDataMng.ReadExcel(builder.toString(),user.getRealname());
		modelMap.put("path", builder.toString());
		return "redirect:v_list.do";
    }
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/house_data/v_delete.do")
	public String delete(Integer[] ids){
		cmsHouseDataMng.delete(ids);
		return "redirect:v_list.do";
	}
	
	/**
	 * 区域统计
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/house_data/v_rangeStatistics.do")
	public String rangeStatistics(String name,String area,String seller,
	        String license,String use,String building,
			   Date begin,Date end,ModelMap modelMap){
		List<Object[]> list = cmsHouseDataMng.rangeStatistics(name, area, seller, license, use, building, begin, end);
		List<Object[]> map = new ArrayList<Object[]>();
		DecimalFormat format = new DecimalFormat("#");
		for(int i=0;i<list.size();i++){
			Object[] object = new Object[11];
			object[0] = list.get(i)[0];
			object[1] = list.get(i)[1];
			object[2] = format.format(Double.parseDouble(String.valueOf(list.get(i)[2])));
			object[3] = format.format(Double.parseDouble(String.valueOf(list.get(i)[3])));
			object[4] = format.format(Double.parseDouble(String.valueOf(list.get(i)[4])));
			object[5] = format.format((Double.parseDouble(String.valueOf(list.get(i)[3])) - 
				    Double.parseDouble(String.valueOf(list.get(i)[4])))/Double.parseDouble(String.valueOf(list.get(i)[4]))*100);
			object[6] = format.format(Double.parseDouble(String.valueOf(list.get(i)[3])) / Double.parseDouble(String.valueOf(list.get(i)[1])));
			object[7] = format.format(Double.parseDouble(String.valueOf(list.get(i)[2])) / Double.parseDouble(String.valueOf(list.get(i)[3])));
			object[8] = format.format(Double.parseDouble(String.valueOf(list.get(i)[2])) / Double.parseDouble(String.valueOf(list.get(i)[4])));
			object[9] = format.format(Double.parseDouble(String.valueOf(list.get(i)[3])) / Double.parseDouble(String.valueOf(list.get(i)[1])));
			object[10] = format.format(Double.parseDouble(String.valueOf(list.get(i)[4])) / Double.parseDouble(String.valueOf(list.get(i)[1])));
			map.add(object);
		}
		modelMap.put("range", map);
		return "house/rangeStatistics";
	}
	
	/**
	 * 楼盘统计
	 * @param begin
	 * @param end
	 * @param pageNo
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/house_data/v_propertyStatistics.do")
	public String propertyStatistics(String name,String area,String seller,
			        String license,String use,String building,
			   Date begin,Date end,ModelMap modelMap){
		List<Object[]> list = cmsHouseDataMng.propertyStatistics(name, area, seller, license, use, building, begin, end);
		List<Object[]> map = new ArrayList<Object[]>();
		DecimalFormat format = new DecimalFormat("#");
		for(int i=0;i<list.size();i++){
			Object[] object = new Object[14];
			object[0] = list.get(i)[0];
			object[1] = list.get(i)[1];
			object[2] = list.get(i)[2];
			object[3] = list.get(i)[3];
			object[4] = format.format(Double.parseDouble(String.valueOf(list.get(i)[4]))/1000);
			object[5] = format.format(Double.parseDouble(String.valueOf(list.get(i)[5])));
			object[6] = format.format(Double.parseDouble(String.valueOf(list.get(i)[6])));
			object[7] = format.format((Double.parseDouble(String.valueOf(list.get(i)[5])) - 
			    Double.parseDouble(String.valueOf(list.get(i)[6])))/Double.parseDouble(String.valueOf(list.get(i)[5]))*100);
			object[8] = format.format(Double.parseDouble(String.valueOf(list.get(i)[4]))/Double.parseDouble(String.valueOf(list.get(i)[3])));
			object[9] = format.format(Double.parseDouble(String.valueOf(list.get(i)[4]))/Double.parseDouble(String.valueOf(list.get(i)[5])));
			object[10] = format.format(Double.parseDouble(String.valueOf(list.get(i)[4]))/Double.parseDouble(String.valueOf(list.get(i)[6])));
			object[11] = format.format(Double.parseDouble(String.valueOf(list.get(i)[5]))/Double.parseDouble(String.valueOf(list.get(i)[3])));
			object[12] = format.format(Double.parseDouble(String.valueOf(list.get(i)[6]))/Double.parseDouble(String.valueOf(list.get(i)[3])));
			map.add(object);
		}
		modelMap.put("property", map);
		return "house/propertyStatistics";
	}
	
	@Autowired
	private CmsHouseDataMng cmsHouseDataMng;

}
